package com.model.demo.designmodel.adapter.login.v1;

import com.model.demo.designmodel.adapter.login.ResultMsg;
import com.model.demo.designmodel.adapter.login.v1.adapter.LoginAdapter;
import com.model.demo.designmodel.adapter.login.v1.adapter.LoginForQQAdapter;
import com.model.demo.designmodel.adapter.login.v1.service.SiginService;

public class PassportForThirdAdapter extends SiginService implements IPassportForThird {
    @Override
    public ResultMsg loginForQQ(String id) {
//        LoginAdapter adapter = new LoginForQQAdapter();
//        if(adapter.support(adapter)){
//            return  adapter.login(id, adapter);
//        }
        return processLogin(id,LoginForQQAdapter.class);
    }

    @Override
    public ResultMsg loginForWechat(String id) {
        return null;
    }

    @Override
    public ResultMsg loginForToken(String token) {
        return null;
    }

    @Override
    public ResultMsg loginForTelphone(String telphone, String code) {
        return null;
    }

    @Override
    public ResultMsg loginForRegist(String username, String passport) {
        return null;
    }

    public ResultMsg processLogin(String key, Class<? extends LoginAdapter> clazz){
        try {
            //适配器不一定要实现接口
            LoginAdapter adapter = clazz.newInstance();

            //判断传过来的适配器是否能处理指定的逻辑
            if(adapter.support(adapter)){
                return adapter.login(key,adapter);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
